using System;
using UnityEngine;

public class Product
{
	public string id;

	[SerializeField]
	protected MoneyType priceIn;

	[SerializeField]
	protected SaveInt price;

	public bool availableForBuy = true;

	public float sortWeight;

	public string texture;

	public string nameForFlurry;

	public bool isTemporary;

	[Range(0f, 7f)]
	public int tier;

	public bool bestPrice;

	public bool newProduct;

	[NonSerialized]
	private float salePercent;

	[SerializeField]
	private bool best;

	[NonSerialized]
	private bool bestJSON;

	[NonSerialized]
	[HideInInspector]
	public string categoryName;

	[NonSerialized]
	[HideInInspector]
	public string subCategoryName;

	public RareType rare;

	[NonSerialized]
	[HideInInspector]
	public Product nextProduct;

	[NonSerialized]
	[HideInInspector]
	public Product prevProduct;

	public string nextProductId;

	public string prevProductId;

	private bool _purchased;

	[NonSerialized]
	protected bool equiped;

	public MoneyType PriceIn
	{
		get
		{
			if (sale != null)
			{
				return sale.newCurrency;
			}
			return priceIn;
		}
	}

	public int Price
	{
		get
		{
			if (sale != null && sale.newPrice > 0)
			{
				return sale.newPrice;
			}
			return price;
		}
	}

	public int MarketPrice
	{
		get
		{
			return price;
		}
	}

	public virtual bool equippable
	{
		get
		{
			return true;
		}
	}

	public bool IsVehicle
	{
		get
		{
			return this is ProductCar || this is ProductTank;
		}
	}

	public MarketSale sale { get; private set; }

	public float SalePercent
	{
		get
		{
			if (sale != null)
			{
				return sale.discount;
			}
			return salePercent;
		}
	}

	public bool Best
	{
		get
		{
			return best || bestJSON;
		}
	}

	public virtual bool Purchased
	{
		get
		{
			if (isTemporary)
			{
				return TemporalProductManager.IsAvailable(id);
			}
			return _purchased || ((int)price == 0 && availableForBuy);
		}
		set
		{
			if (!isTemporary)
			{
				_purchased = value;
			}
		}
	}

	public virtual bool Equipped
	{
		get
		{
			return equiped;
		}
	}

	public event Action<bool> OnEquippedStateChanged;

	public Product(string id_G, MoneyType priceIng_G, int price_G, bool availableForBuy_G, float sortWeight_G, string texture_G, string nameForFlurry_G, bool isTemporary_G, int tier_G, bool bestPrice_G, bool newProduct_G, string nextProductId_G, string prevProductId_G, string categoryName_G, string subCategoryName_G, RareType rare_G)
	{
		id = id_G;
		priceIn = priceIng_G;
		price = price_G;
		availableForBuy = availableForBuy_G;
		sortWeight = sortWeight_G;
		texture = texture_G;
		nameForFlurry = nameForFlurry_G;
		isTemporary = isTemporary_G;
		tier = tier_G;
		newProduct = newProduct_G;
		nextProductId = nextProductId_G;
		prevProductId = prevProductId_G;
		categoryName = categoryName_G;
		subCategoryName = subCategoryName_G;
		rare = rare_G;
		best = newProduct_G || bestPrice_G;
		if (!isTemporary)
		{
			_purchased = BCWStorage.Get<bool>(id);
		}
	}

	[Obsolete("Только для google docs")]
	public void SetPrice(int value)
	{
		price = value;
	}

	public void SetPriceIn(MoneyType moneyType)
	{
		priceIn = moneyType;
	}

	public void SetSalePercent(float percent)
	{
		salePercent = percent;
	}

	public void SetBestProduct(bool value)
	{
		bestJSON = value;
	}

	public virtual void BuyProduct(params object[] args)
	{
		BCWStorage.Set(id, true, true);
		_purchased = true;
	}

	public void ResetProduct()
	{
		BCWStorage.Set(id, false, true);
		_purchased = false;
	}

	public virtual void Init(string _categoryName, string _subCategoryName)
	{
		categoryName = _categoryName;
		subCategoryName = _subCategoryName;
		if (!isTemporary)
		{
			_purchased = BCWStorage.Get<bool>(id);
		}
	}

	public void ApplySale()
	{
		if (!Purchased)
		{
			sale = MarketSaleController.instance.GetSaleForProduct(id);
		}
		else
		{
			sale = MarketSaleController.instance.GetSaleForProduct(nextProductId);
		}
		if (sale != null && !sale.oldCurrency.HasValue)
		{
			sale.oldCurrency = priceIn;
		}
	}

	[Obsolete("Вызывать только MarketController")]
	public virtual bool Equip()
	{
		equiped = true;
		Save.SaveString(GetEquipedKey(), id);
		if (this.OnEquippedStateChanged != null)
		{
			this.OnEquippedStateChanged(true);
		}
		if (categoryName.Equals("Looks") && BCWServer.instance != null)
		{
			BCWServer.instance.UpdatePlayerData();
		}
		return true;
	}

	[Obsolete("Вызывать только из MarketController")]
	public virtual bool UnEquip()
	{
		if (Load.LoadString(GetEquipedKey()).Equals(id))
		{
			equiped = false;
			Save.SaveString(GetEquipedKey(), string.Empty);
			if (this.OnEquippedStateChanged != null)
			{
				this.OnEquippedStateChanged(false);
			}
			return true;
		}
		return false;
	}

	public virtual void SetEquiped()
	{
		equiped = true;
	}

	protected virtual string GetEquipedKey()
	{
		return string.Empty;
	}
}
